/* Copyright(C): Leaf Johnson 2009,2010

    This file is part of leaf-lib.

    leaf-lib is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    leaf-lib is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with leaf-lib.  If not, see <http://www.gnu.org/licenses/>.


 * 
 * 
 */


#ifndef LCD_H_INCLUDED
#define LCD_H_INCLUDED
#include <avr/io.h>
#include <leaf/types.h>
#include <avr/pgmspace.h>
/* 默认的接口方式:
 * 连接方式 6线
 * D4~D7 - PC4~7
 * RS    - PC2
 * EN    - PC3
 */
#define LCD_CONNTYPE_L4 3
#define LCD_CONNTYPE_H4 2
#define LCD_CONNTYPE_8  1

/* 液晶显示模块，在此设置端口 */
#define LCD_CONFIGED   1
#define LCD_EN_PORT    PORTA
#define LCD_EN_DDR     DDRA
#define LCD_RS_PORT    PORTA
#define LCD_RS_DDR     DDRA
#define LCD_RS         (1<<PA2)
#define LCD_EN         (1<<PA3)
#define LCD_DATA_DDR   DDRA
#define LCD_DATA_PORT  PORTA
#define LCD_DATA_PIN   PINA
#define LCD_CONN_TYPE  LCD_CONNTYPE_H4

#if (LCD_CONN_TYPE == LCD_CONNTYPE_L4)
#define	LCD_DATA_MASK  0x0f;
#elif (LCD_CONN_TYPE == LCD_CONNTYPE_H4)
#define	LCD_DATA_MASK  0xf0;
#elif (LCD_CONN_TYPE == LCD_CONNTYPE_8)
#define	LCD_DATA_MASK  0xff;
#endif

/* LCD Commands */
#define LCD_CLEAR 0x01

void  lcd_init(void);
void  lcd_command(uint8_t command) ;
void  lcd_send_data(uint8_t data);
void  lcd_set_xy (uint8_t x, uint8_t y);
void  inline lcd_putc(const char c);
void  lcd_puts(const char* s);
void  inline lcd_poss(uint8_t X,uint8_t Y,const char *s);
void  inline lcd_posc(uint8_t X,uint8_t Y, char data);
void lcd_clear( uint8_t row, uint8_t cow_st, uint8_t cow_ed );

#ifdef __PGMSPACE_H_
void lcd_puts_P( PGM_P s );
void lcd_poss_P( uint8_t x, uint8_t y, PGM_P s );
#define LCD_STAT(info) lcd_poss_P( 10, 0, PSTR(info) )
#define LCD_ERR( info ) lcd_poss_P( 10, 1, PSTR(info) )
#endif

#endif
